﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using ECC_EXT_DAO.Dao;
using ECC_EXT_Entity;

namespace ECCSystem
{
    public partial class frm_Ext_OFFAnnotation : Form
    {
        public frm_Ext_OFFAnnotation()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.LoadDataGridView();
        }

        private void LoadDataGridView()
        {
            OFFAnnotationDao _offAnnotationDao = new OFFAnnotationDao();
            OFFAnnotationEntitySearch _search = new OFFAnnotationEntitySearch();

            _search.RecordDateFrom = this.dtp_RecordDateFrom.Value.Date;
            _search.RecordDateTo = this.dtp_RecordDateTo.Value.Date;

            _search.RecordUser = this.textBox_RecoedUser.Text;

            _search.Status =
                this.comboBox_Status.Items[this.comboBox_Status.SelectedIndex] as DICOFFAnnotatonStatusEntity;

            this.dataGridView1.DataSource = this.ConvertDto(_offAnnotationDao.Search(_search));
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                string strId =
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

                frm_Ext_Merchandise _frm = new frm_Ext_Merchandise();
                _frm.pAnnotationID = long.Parse(strId);
                this.Hide();
                if( DialogResult.OK ==  _frm.ShowDialog())
                    this.LoadDataGridView();
                this.Show();
            }
        }

        private IList<OFFAnnotatioGridViewDto> ConvertDto(IList<OFFAnnotationEntity> _listSrc)
        {
            IList<OFFAnnotatioGridViewDto> _list = new List<OFFAnnotatioGridViewDto>();

            foreach (var item in _listSrc)
            {
                _list.Add(new OFFAnnotatioGridViewDto(item));
            }
            return _list;
        }

        private void frm_Ext_OFFAnnotation_Load(object sender, EventArgs e)
        {
            this.InitComboxStatus();
            this.LoadDataGridView();

        }

        private void InitComboxStatus()
        {
            //Init status combox
            DICOFFAnnotatonStatusDao _dicoffAnnotatonStatusDao = new DICOFFAnnotatonStatusDao();

            IList<DICOFFAnnotatonStatusEntity> _list =
                _dicoffAnnotatonStatusDao.Search();

            //DICOFFAnnotatonStatusEntity _entity = new DICOFFAnnotatonStatusEntity();
            //_entity.ID = -1;
            //_entity.Name = "";
            //this.comboBox_Status.Items.Add(_entity);

            foreach (var dicoffAnnotatonStatusEntity in _list)
            {
                this.comboBox_Status.Items.Add(dicoffAnnotatonStatusEntity);
            }

            this.comboBox_Status.DisplayMember = "Name";
            this.comboBox_Status.ValueMember = "ID";

            this.comboBox_Status.SelectedIndex = 0;
        }


    }

    internal class OFFAnnotatioGridViewDto
    {
        public OFFAnnotatioGridViewDto(OFFAnnotationEntity _entity)
        {

            Id = _entity.ID;
            RecordUser = _entity.RecordUser;
            RecordDate = _entity.RecordDate;
            CaseNo = _entity.CaseNo;
            AccountNo = _entity.AccountNo;
            //CategoryName = _entity.DICCategory.Name;
            //CategoryCode = _entity.DICCategory.Code;
            FollowUser = _entity.FollowUser;
            FollowDate = _entity.FollowDate.HasValue ? _entity.FollowDate.Value.ToString() : "";

            if(_entity.dicOFFAnnotatonStatus != null)
                Status = _entity.dicOFFAnnotatonStatus.Name;
        }

        public long Id { get; set; }

        public string RecordUser { get; set; }

        public DateTime? RecordDate { get; set; }

        public string CaseNo { get; set; }

        public string AccountNo { get; set; }

        public String FollowUser { get; set; }

        public String FollowDate { get; set; }

        public String Status { get; set; }
    }
}
